package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * Created by shaunaks on 25/02/14.
 */
public class Librarian
{

        Register register=new Register();

        UserAccount userAccount1=new UserAccount("001-0001","sample_pass1","Shaunak","shaunak.s09@gmail.com","8884055989");
        UserAccount userAccount2=new UserAccount("002-0002","sample_pass2","Sinha","shaunaks@thoughtworks.com","9739591769");
        ArrayList<UserAccount> userAccountList=new ArrayList<UserAccount>();

        UserAccount currentUserAccount;

    public Librarian()
    {
        userAccountList.add(userAccount1);
        userAccountList.add(userAccount2);
    }


    public void addBook()
        {
            System.out.println("Please enter the details of the book in the format[isbn_number:book_name:book_author]");
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String bookAttributes=null;
            try {
                bookAttributes=br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            boolean result=register.addBookToRegister(bookAttributes);
            if(result==true)
                System.out.println("Book added successfully");
            else
                System.out.println("Could not add book! Try again");

        }



        public void listAvailableBooks()
        {
            if(register.getListOfAvailableBooks().size()==0)
                System.out.println("No books available in library!");
            else
            {
                ArrayList<Book> bookList;
                bookList=register.getListOfAvailableBooks();
                for(Book book:bookList)
                {
                    System.out.println(book.toString());
                }
            }
        }



        public void returnBook()
        {
            System.out.println("Please enter the ISBN number of the book you are returning to Biblioteca");
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String bookIsbn=null;
            try {
                 bookIsbn=br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }

            boolean resultOfReturningBookToRegister=false;


            for(Book currentUserBorrowedBook:currentUserAccount.getListOfBorrowedBooks())
            {
                if(bookIsbn.equals(currentUserBorrowedBook.getIsbn()))
                {
                    currentUserAccount.removeBookOrMovieFromRespectiveList(currentUserBorrowedBook);
                    resultOfReturningBookToRegister=register.returnBookToRegister(bookIsbn);
                    System.out.println("Book returned successfully! Thank you.");
                    break;
                }
            }


            if(resultOfReturningBookToRegister==false)
                System.out.println("That is not a valid book to return! :-(");

        }



        public void checkOutBook()
        {
            System.out.println("Enter the ISBN number of the book that you want to borrow");
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String bookIsbn=null;
            try {
                bookIsbn=br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }

            Book checkedOutBook=register.checkOutBookFromRegister((bookIsbn));
            if(checkedOutBook!=null)
            {
                currentUserAccount.addBookOrMovieToRespectiveList(checkedOutBook);
                System.out.println("Book checked out successfully!\n Thank you. Enjoy the book!!!");
            }
            else
                System.out.println("That book is not available. :-(");

        }



        public Book createBookObject(String bookAttributes)  //where is it being used??
        {
            String [] separatedBookAttributes=bookAttributes.split(":");
            Book book=new Book(separatedBookAttributes[0],separatedBookAttributes[1],separatedBookAttributes[2]);
            return book;
        }


        public void viewListOfBookBorrowers()
        {
            if(register.getListOfBorrowedBooks().size()==0)
            {
                System.out.println("No borrowers yet!");
            }
            else
            {
                System.out.println("*** LIST OF BOOK BORROWERS ***");
                for(UserAccount userAccount: userAccountList)
                {
                    if(userAccount.getListOfBorrowedBooks().size()!=0)
                    {
                        System.out.println("\nUser Library Number:"+userAccount.getLibraryNumber()+"\nUser Name:"+userAccount.getUserName());
                        System.out.println("Books borrowed:\n");
                        for(Book borrowedBook:userAccount.getListOfBorrowedBooks())
                        {
                            System.out.println(borrowedBook.toString());
                        }
                    }
                }
            }
        }


    public void addMovie()
    {
        System.out.println("Please enter the details of the movie in the format[name:director's name:year of release:rating of movie]");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String movieAttributes=null;
        try {
            movieAttributes=br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        boolean result=register.addMovieToRegister(movieAttributes);
        if(result==true)
            System.out.println("Movie added successfully");
        else
            System.out.println("Could not add movie! Try again");


    }


    public void listAvailableMovies()
    {
        if(register.getListOfAvailableMovies().size()==0)
            System.out.println("No movies available in library!");
        else
        {
            ArrayList<Movie> movieList;
            movieList=register.getListOfAvailableMovies();
            for(Movie movie:movieList)
            {
                System.out.println(movie.toString());
            }
        }
    }

    public void returnMovie()
    {
        System.out.println("Please enter the name of the movie you are returning to Biblioteca");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String movieName=null;
        try {
            movieName=br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

        boolean resultOfReturningMovieToRegister=false;


        for(Movie currentUserBorrowedMovie:currentUserAccount.getListOfBorrowedMovies())
        {
            if(movieName.equals(currentUserBorrowedMovie.getName()))
            {
                currentUserAccount.removeBookOrMovieFromRespectiveList(currentUserBorrowedMovie);
                resultOfReturningMovieToRegister=register.returnMovieToRegister(movieName);
                System.out.println("Movie returned successfully! Thank you.");
                break;
            }
        }


        if(resultOfReturningMovieToRegister==false)
            System.out.println("That is not a valid movie to return! :-(");

    }

    public void checkOutMovie()
    {
        System.out.println("Enter the name of the movie that you want to borrow");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String movieName=null;
        try {
            movieName=br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Movie checkedOutMovie=register.checkOutMovieFromRegister((movieName));
        if(checkedOutMovie!=null)
        {
            currentUserAccount.addBookOrMovieToRespectiveList(checkedOutMovie);
            System.out.println("Movie checked out successfully!\n Thank you. Enjoy the movie!!!");
        }
        else
            System.out.println("That movie is not available. :-(");

    }

    public void viewListOfMovieBorrowers()
    {
        if(register.getListOfBorrowedMovies().size()==0)
        {
            System.out.println("No borrowers yet!");
        }
        else
        {
            System.out.println("*** LIST OF MOVIE BORROWERS ***");
            for(UserAccount userAccount: userAccountList)
            {
                if(userAccount.getListOfBorrowedMovies().size()!=0)
                {
                    System.out.println("\nUser Library Number:"+userAccount.getLibraryNumber()+"\nUser Name:"+userAccount.getUserName());
                    System.out.println("Movies borrowed:");
                    for(Movie borrowedMovie:userAccount.getListOfBorrowedMovies())
                    {
                        System.out.println(borrowedMovie.toString());
                    }
                }
            }
        }

    }


    public UserAccount searchForUserAccount (String userCredentials) {

        String userLibraryNumber,userPassword;
        String []separatedUserCredentials=userCredentials.split(":");

        userLibraryNumber=separatedUserCredentials[0];
        userPassword=separatedUserCredentials[1];

        for(UserAccount userAccount :userAccountList)
        {
            if(userLibraryNumber.equals(userAccount.getLibraryNumber()) && userPassword.equals(userAccount.getLoginPassword()))
            {
                currentUserAccount=userAccount;
                return userAccount;
            }
        }
        return null;

    }
}
